package com.wlts.wzy.KSOrderModule.managerController;


import com.wlts.wzy.Base.BaseEntity;
import com.wlts.wzy.KSOrderModule.entity.KsOrders;
import com.wlts.wzy.KSOrderModule.service.IKsOrdersService;
import com.wlts.wzy.KSUserModule.entity.KsStoreUser;
import com.wlts.wzy.KSUserModule.service.IKsStoreUserService;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.api.request.ks.GetMyKsOrdersRequest;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.expand.MyPage;
import com.wlts.wzy.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 快手订单表 前端控制器
 * </p>
 *
 * @author zhiyong
 * @since 2021-02-02
 */
@Api(tags = "快手订单表 后台管理控制器")
@RestController
@RequestMapping("/manager/ks-orders")
public class KsOrdersManagerController {
    @Autowired
    private IKsOrdersService ksOrdersService;
    @Autowired
    private IKsStoreUserService ksStoreUserService;

    @ApiOperation(value = "获得快手订单 管理员")
    @RequestMapping(value = "getMyKsOrders", method = RequestMethod.POST)
    public Result<MyPage<KsOrders>> getMyKsOrders(@RequestBody GetMyKsOrdersRequest request) {

     //   System.out.println(request.toString());

        MyLambdaQueryWrapper<KsOrders> wrapper = new MyLambdaQueryWrapper<>();


        if(StringUtils.isNotEmpty(request.getUserUuid())){
            MyLambdaQueryWrapper<KsStoreUser> userWrapper = new MyLambdaQueryWrapper<>();

            userWrapper.eq(KsStoreUser::getUuid, request.getUserUuid());

            KsStoreUser ksStoreUser = ksStoreUserService.getOne(userWrapper);

            if (ksStoreUser == null) {
                return Result.buildResult(500,"用户不存在");
            }

            if (ksStoreUser.getKsSellerId() == null) {
                return Result.buildResult(500,"用户未授权");
            }
            wrapper.eq(KsOrders::getSellerId, ksStoreUser.getKsSellerId());
        }

        if (request.getOid() != null) {
            wrapper.eq(KsOrders::getOid, request.getOid());
        }
        if (request.getPutLmStatus() != null) {
            wrapper.eq(KsOrders::getPutLmStatus, request.getPutLmStatus());
        }
        if (request.getSendStatus() != null) {
            wrapper.eq(KsOrders::getSendStatus, request.getSendStatus());
        }
        if (request.getStatus() != null) {
            wrapper.eq(KsOrders::getStatus, request.getStatus());
        }
        if (request.getSelfOrder() != null) {
            wrapper.eq(KsOrders::getSelfOrder, request.getSelfOrder());
        }
        if (StringUtils.isNotEmpty(request.getMobile())) {
            wrapper.like(KsOrders::getMobile, request.getMobile());
        }

        if (request.getRefund() != null) {
            wrapper.eq(KsOrders::getRefund, request.getRefund());
        }
        wrapper.orderByDesc(BaseEntity::getCreateDate);
        MyPage<KsOrders> lmSelfGoodsList = ksOrdersService.page(wrapper, request.getCurrentPage(), request.getPageSize(), true);

        return Result.buildResult(lmSelfGoodsList);

    }


}
